#!/usr/bin/perl

use DBI;
use strict;
use POSIX;

# Some setup variables
my $dsn="dbi:Pg:dbname=flux_monitoring;host=localhost;port=5432";
my $user="postgres";
my $password="";

my $CABB_output_directory="/DATA/ARCHIVE_1/ATCA/archive";

# Set up a connection to the database listing the RPFITS files that need to be
# processed.
my $dbh=DBI->connect($dsn,$user,$password,{ RaiseError => 0, AutoCommit => 1});

# get the directory listing of all the CABB data files
open(DIR,"-|")||exec "ls -rt $CABB_output_directory";
my @potential_files;
while(<DIR>){
    chomp;
    my $check_file=$_;
    print "$check_file\n";
    # check that the date is after 2009-05-01
    my ($year,$month,$day,$time,$project);
    if ($check_file=~m/(\d\d\d\d)-(\d\d)-(\d\d)_(\d\d\d\d)\.(.*)/){
	($year,$month,$day,$time,$project)=($1,$2,$3,$4,$5);
    } else {
	next;
    }
    if ($year<2009){
	next;
    }
    if (($year==2009)&&($month<5)){
	next;
    }
    if (($project!~/^C/)||($project eq "C999")){
	next;
    }
    print "$year/$month/$day $time $project\n";
    # if we're here, we could add the file to the list
    push @potential_files,$check_file;
}
close(DIR);

# now go through the potential files, but don't include the very last
# file, as that is still likely being written to
my $rpi_query="INSERT INTO rpfits_files VALUES(?,?,FALSE)";
my $rpi=$dbh->prepare($rpi_query);
for (my $i=0;$i<$#potential_files;$i++){
    # we don't need to check for files already in the database, as
    # we won't be allowed to enter the same FITS file twice anyway
    print "adding $potential_files[$i]\n";
    $rpi->execute($potential_files[$i],$CABB_output_directory);
}

#my $done=0;
#while ($done==0){
#    my $nels=$#potential_files;
#    my $getel;
#    if ($nels>0){
#	$getel=floor(rand($nels));
#    } else {
#	$getel=0;
#	$done=1;
#    }
#    print "adding $potential_files[$getel]\n";
#    $rpi->execute($potential_files[$getel],$CABB_output_directory);
#    splice @potential_files,$getel,1;
#}

$rpi->finish;
$dbh->disconnect;
